import json

from backend.models import Project, Performance, Achievement
from .util import *
from django.core.paginator import Paginator


def save(request):
    """
    新增 / 修改 项目信息
    :param request: Post请求
    :return: Json 信息
    """
    if request.method != 'POST':
        return format_response(WRONG_METHOD)

    project_id, \
    serial, \
    decree, \
    title, \
    introduce, \
    user_id, \
    total_num, \
    start_date, \
    end_date, is_active, status, score = \
        format_param(request,
                     "project_id", "serial", "decree", "title", "introduce",
                     "user_id", "total_num", "start_date", "end_date", "is_active", "status", "score")

    start_date = start_date[:10]
    end_date = end_date[:10]
    if project_id:
        try:
            project = Project.objects.get(id=project_id)
            project.serial = serial
            project.decree = decree
            project.title = title
            project.introduce = introduce
            project.head_user_id = user_id
            project.total_number = total_num
            project.start_date = start_date
            project.end_date = end_date
            project.is_active = is_active
            project.status = status
            project.score = score
            project.save()
        except:
            return format_response(WRONG_PARAM)
    else:
        project = Project(
            serial=serial,
            decree=decree,
            title=title,
            introduce=introduce,
            head_user_id=user_id,
            total_number=total_num,
            start_date=start_date,
            end_date=end_date,
        )
        print(project.save())
    return format_response(SUCCESS, project.id)


def delete(request):
    """
    删除项目
    :param request:
    :return:
    """
    if request.method != "POST":
        return format_response(WRONG_METHOD)

    project_id, = format_param(request, "project_id")

    try:
        project = Project.objects.get(id=project_id)
        project.is_active = False
        project.save()
        return format_response(SUCCESS)
    except:
        return format_response(WRONG_POINTER)


def get(request):
    """
    获取项目信息
    :param request:
    :return:
    """
    if request.method != "GET":
        return format_response(WRONG_METHOD)

    project_id = request.GET.get('project_id')
    if not project_id:
        return format_response(WRONG_PARAM)

    try:
        project = Project.objects.get(id=project_id)
        return format_response(SUCCESS, project.to_json())
    except:
        return format_response(WRONG_POINTER)


def project_list(request):
    """
    获取项目列表
    :param request:
    :return:
    """
    if request.method != 'GET':
        return format_response(WRONG_METHOD)
    print(request.session.items())
    try:
        user_id = request.session.get('user_id')
    except:
        return format_response(WRONG_PARAM)

    size = request.GET.get('size')
    page = request.GET.get('page')

    if request.session.get('is_super'):
        projects = Project.objects.all().filter(is_active=True)
    else:
        projects = Project.objects.all().filter(head_user_id=user_id, is_active=True)
    data = []
    paginator = Paginator(projects, size)
    projects = paginator.page(page)
    total_page = paginator.num_pages
    for item in projects:
        head_user_id = None
        head_user_name = None
        if item.head_user:
            head_user_id = item.head_user.id
            head_user_name = item.head_user.get_full_name()
        data.append({
            'project_id': item.id,
            'title': item.title,
            'serial': item.serial,
            'decree': item.decree,
            'introduce': item.introduce,
            'head_user_id': head_user_id,
            'head_user': head_user_name,
            'total_number': item.total_number,
            'start_date': item.start_date.strftime("%Y-%m-%d"),
            'end_date': item.end_date.strftime("%Y-%m-%d"),
        })
    result = {
        "list": data,
        "total_page": total_page,
    }
    return format_response(SUCCESS, result)


def get_performance_of_project(request):
    project_id = request.GET.get('project_id')
    p = Performance.objects.all().filter(project_id=project_id)
    tmp = []
    for item1 in p:
        tmp.append(item1.user)
    a = Achievement.objects.all().filter(project_id=project_id)
    for item2 in a:
        tmp.append(item2.user)
    users = list(set(tmp))
    data = []
    for item in users:
        performance_num = item.get_performamce(project_id)
        achievement_num = item.get_achievement(project_id)
        argument_num = item.get_argument(project_id)
        data.append({
            'user_id': item.id,
            'nickname': item.get_full_name(),
            'performance': performance_num,
            'achievement': achievement_num,
            'argument': argument_num,
            'total': performance_num + achievement_num + argument_num
        })

    return format_response(SUCCESS, data)
